package cn.com.surker.spring;

import cn.com.surker.common.Code;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * RestResultDto
 * 封装的返回类
 */
@Data
public class RestResultDto<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 系统内部错误
     */
    public static final int ERROR_SYS = -1;
    /**
     * 成功
     */
    public static final int SUCCESS = 0;
    /**
     * 业务失败
     */
    public static final int FAIL_SERVICE = 1;
    /**
     * 会话失败
     */
    public static final int FAIL_AUTH_SESSION = 2;
    /**
     * 需升级客户端
     */
    public static final int FAIL_UPGRADE = 3;
    /**
     * 鉴权失败
     */
    public static final int FAIL_AUTH = 4;


    /**
     * 返回码
     */
    @Getter
    @Setter
    private int code = SUCCESS;

    /**
     * 返回信息
     */
    @Getter
    @Setter
    private String msg;

    /**
     * 数据总条数
     */
    @Getter
    @Setter
    private Integer total;

    /**
     * 返回数据
     */
    private T data;

    public RestResultDto() {

    }

    public RestResultDto(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public RestResultDto(Integer code, String msg, Integer total, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.total = total;
    }

    public RestResultDto(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回执行成功结果
     *
     * @return
     */
    public static RestResultDto success() {
        return new RestResultDto();
    }

    /**
     * 返回执行成功结果
     *
     * @param msg 执行后的消息
     * @return
     */
    public static RestResultDto success(String msg) {
        RestResultDto result = new RestResultDto();
        result.setMsg(msg);

        return result;
    }

    /**
     * 返回执行成功结果
     *
     * @param data 返回的具体业务内容
     * @return
     */
    public static <T> RestResultDto<T> success(T data) {
        return success(null, data);
    }


    /**
     * 返回执行成功结果
     *
     * @param msg  执行后的消息
     * @param data 返回的具体业务内容
     * @return
     */
    public static <T> RestResultDto<T> success(String msg, T data) {
        RestResultDto result = new RestResultDto();
        result.setMsg(msg);
        result.setData(data);

        return result;
    }

    /**
     * @param status
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> RestResultDto<T> success(int status, String msg) {
        RestResultDto result = new RestResultDto();
        result.setMsg(msg);
        result.setCode(status);

        return result;
    }

    /**
     * @param total
     * @param <T>
     * @return
     */
    public static <T> RestResultDto<T> success(int total, T data) {
        RestResultDto result = new RestResultDto();
        result.setTotal(total);
        result.setData(data);

        return result;
    }

    /**
     * 返回执行成功结果
     *
     * @param status
     * @param msg    执行后的消息
     * @param data   返回的具体业务内容
     * @return
     */
    public static <T> RestResultDto<T> success(int status, String msg, T data) {
        RestResultDto result = new RestResultDto();
        result.setCode(status);
        result.setMsg(msg);
        result.setData(data);

        return result;
    }

    /**
     * 返回执行失败结果
     *
     * @return
     */
    public static RestResultDto fail() {
        RestResultDto result = new RestResultDto();
        result.setCode(FAIL_SERVICE);

        return result;
    }

    /**
     * 返回执行失败结果
     *
     * @param msg 执行后的消息
     * @return
     */
    public static RestResultDto fail(String msg) {
        RestResultDto result = new RestResultDto();
        result.setMsg(msg);
        result.setCode(FAIL_SERVICE);

        return result;
    }

	/**
	 * 返回执行失败结果，根据枚举类型设置返回码以及信息
	 * @param code
	 * @return
	 */
	public static RestResultDto fail(Code code) {
		RestResultDto result = new RestResultDto();
		result.setCode(code.getCode());
		result.setMsg(code.getMsg());
		return result;
	}

	/**
	 * 根据返回码设置返回信息
	 * @param code
	 * @return
	 */
	public static RestResultDto complete(Integer code){
		RestResultDto result = new RestResultDto();
		String msg = Code.getMsg(code);
		result.setCode(code);
		result.setMsg(msg);
		return result;
	}

    /**
     * 返回执行失败结果
     *
     * @param data 返回的具体业务内容
     * @return
     */
    public static <T> RestResultDto<T> fail(T data) {
        RestResultDto result = new RestResultDto();
        result.setData(data);
        result.setCode(FAIL_SERVICE);
        return result;
    }

    /**
     * 返回执行失败结果
     *
     * @param msg  执行后的消息
     * @param data 返回的具体业务内容
     * @return
     */
    public static <T> RestResultDto<T> fail(String msg, T data) {
        return new RestResultDto(FAIL_SERVICE, msg, data);
    }

    /**
     * 返回执行失败结果
     *
     * @param status
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> RestResultDto<T> fail(int status, String msg) {
        return new RestResultDto(status, msg, null);
    }

    /**
     * 返回执行失败结果
     *
     * @param status
     * @param data
     * @param <T>
     * @return
     */
    public static <T> RestResultDto<T> fail(int status, T data) {
        return new RestResultDto(status, null, data);
    }

    /**
     * 返回执行失败结果
     *
     * @param status 执行状态
     * @param msg    执行后的消息
     * @param data   返回的具体业务内容
     * @return
     */
    public static <T> RestResultDto<T> fail(int status, String msg, T data) {
        return new RestResultDto(status, msg, data);
    }

}
